package com.capgemini.simpleappblanche.domain.security;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import org.springframework.security.core.GrantedAuthority;

import com.capgemini.simpleappblanche.domain.Agency;

@SuppressWarnings("serial")
@Getter
@Setter
@ToString(of={"id","name","agency.id"})
@EqualsAndHashCode(of={"id"})
@Entity
@Table(name = "ROLE")
public class Role implements GrantedAuthority {
    
    // Technical id
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
	private Long id;
	
	@Column( name = "NAME" , nullable = false)
	private String name;
	
    @ManyToOne( fetch = FetchType.EAGER)
    @JoinColumn(name="AGENCY_ID", nullable = true)
	private Agency agency;
	
	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(
	        name = "ROLE_PERMISSION",
	        joinColumns = { @JoinColumn(name = "ROLE_ID") },
	        inverseJoinColumns = { @JoinColumn(name = "PERMISSION_ID") })
    private Set<Permission> permissions = new HashSet<Permission>();

    /**
     * {@inheritDoc}
     * @see org.springframework.security.core.GrantedAuthority#getAuthority()
     */
    @Override
    @Transient
    public String getAuthority() {
        return name;
    }
	
}
